//
//  IBMagicStar.m
//  烟花特效
//
//  Created by apple on 15/7/9.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "IBMagicStar.h"
#import <CoreGraphics/CoreGraphics.h>

@interface IBMagicStar ()
{
    CAEmitterLayer * _startEmitterLayer;
}
@end

@implementation IBMagicStar

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self animationEmitter];
    }
    return self;
}

+(Class)layerClass
{
    return [CAEmitterLayer class];
}

//配置显示动画
-(void)animationEmitter
{
    _startEmitterLayer = [CAEmitterLayer layer];
    _startEmitterLayer.emitterPosition = CGPointMake(self.bounds.size.width / 2, 0);
    _startEmitterLayer.emitterSize = CGSizeMake(self.bounds.size.width , self.bounds.size.height / 10); //粒子显示宽度
    _startEmitterLayer.emitterMode = kCAEmitterLayerOutline; //发射模式
    _startEmitterLayer.emitterShape = kCAEmitterLayerRectangle; //形状
    _startEmitterLayer.renderMode = kCAEmitterLayerAdditive; //递增模式
    
    //黄色星星
    CAEmitterCell * starCell = [CAEmitterCell emitterCell];
    starCell.contents = (id)[[UIImage imageNamed:@"fireStar.png"]CGImage];
    starCell.name = @"star";
    starCell.birthRate = 30;
    starCell.lifetime = 2;
    starCell.lifetimeRange = 0.5;
    starCell.yAcceleration = self.bounds.size.height * 2 / 3; //下落方向
    starCell.scale = 0.3; //缩放
    starCell.scaleRange = 0.2;
    starCell.alphaRange = 1; //alpha变化
    starCell.alphaSpeed = -0.7;
    starCell.color = [[UIColor yellowColor] CGColor]; //颜色
    starCell.redRange = 0.5;
    starCell.greenRange = 0.5;
    starCell.blueRange = 0.5;
    starCell.spin = 2;
    starCell.spinRange = 0.7;
    
    //白色菱形
    CAEmitterCell * starCell1 = [CAEmitterCell emitterCell];
    starCell1.contents = (id)[[UIImage imageNamed:@"lingxing.png"]CGImage];
    starCell1.name = @"star";
    starCell1.birthRate = 5;
    starCell1.lifetime = 1.5;
    starCell1.lifetimeRange = 1;
    starCell1.yAcceleration = self.bounds.size.height * 2 / 3; //下落方向
    starCell1.scale = 1.3; //缩放
    starCell1.scaleRange = 0.2;
    starCell1.alphaRange = 1; //alpha变化
    starCell1.alphaSpeed = -0.7;
    starCell1.spin = 2;
    starCell1.spinRange = 0.7;
    
    _startEmitterLayer.emitterCells = @[starCell,starCell1];
    [self.layer addSublayer:_startEmitterLayer];
}

-(void)removeByTime:(float)time
{
    [UIView animateWithDuration:time animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
